//
//  SelectTimeTypeView.m
//  WatermarkCamera
//
//  Created by zhibao on 2023/9/14.
//

#import "SelectTimeTypeView.h"
#import "SelectTimeTypeCell.h"
@interface SelectTimeTypeView()<UITableViewDelegate,UITableViewDataSource>{
}
@property (nonatomic,strong) MYTableView *showTableView;
@property(nonatomic,strong)NSArray *dataArray;
@end
@implementation SelectTimeTypeView
- (instancetype)init{
    self = [super initWithFrame:CGRectMake(0, MY_Screen_H, MY_Screen_W, MY_Screen_H)];
    if(self){
        NSString *day = [Helper getCurrentTimes:@"yyyy.MM.dd"];
        NSString *week = [[MYGlobal share] getWeekDay:[NSDate new].weekday];
        NSString *time = [Helper getCurrentTimes:@"HH:mm"];
        NSString *times = [Helper getCurrentTimes:@"HH:mm:ss"];
        _dataArray = @[
            @{
                @"type":@"ABC",
                @"time":[NSString stringWithFormat:@"%@ %@ %@",day,week,time],
            },
            @{
                @"type":@"ACs",
                @"time":[NSString stringWithFormat:@"%@ %@",day,times],
            },
            @{
                @"type":@"AC",
                @"time":[NSString stringWithFormat:@"%@ %@",day,time],
            },
            @{
                @"type":@"AB",
                @"time":[NSString stringWithFormat:@"%@ %@",day,week],
            },
            @{
                @"type":@"BC",
                @"time":[NSString stringWithFormat:@"%@ %@",week,time],
            },
            @{
                @"type":@"A",
                @"time":[NSString stringWithFormat:@"%@",day],
            },
            @{
                @"type":@"B",
                @"time":[NSString stringWithFormat:@"%@",week],
            },
            @{
                @"type":@"C",
                @"time":[NSString stringWithFormat:@"%@",time],
            },
        ];
        self.backgroundColor = MyColorAlpha(@"000000", 0.0);
        [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self];
        [self initView];
    }
    return self;
}

-(void)initView{
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, MY_Screen_H - 557, MY_Screen_W, 64 + 20)];
    topView.layer.cornerRadius = 20;
    topView.backgroundColor = UIColor.whiteColor;
    [self addSubview:topView];
    
    if(topView){
        UILabel *topTitleLabel = [UILabel new];
        topTitleLabel.text = LocalizedString(@"选择时间格式");
        topTitleLabel.font = MY_SCALE_SemiboldFONT(16);
        topTitleLabel.textColor = MYBlock;
        [topView addSubview:topTitleLabel];
        [topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(topView);
            make.top.mas_equalTo(16);
        }];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn setImage:[UIImage imageNamed:@"com_close_qian"] forState:UIControlStateNormal];
        [topView addSubview:cancelBtn];
        [cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        cancelBtn.tag = 1000;
        [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(15);
            make.right.mas_equalTo(-15);
        }];
    }
    
    self.showTableView = [[MYTableView alloc]initWithFrame:CGRectMake(0, topView.bottom - 20, MY_Screen_W, 443) style:UITableViewStylePlain];
    self.showTableView.delegate = self;
    self.showTableView.dataSource = self;
    
    self.showTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.showTableView];
    self.showTableView.rowHeight = 55;
    self.showTableView.backgroundColor = UIColor.whiteColor;
    self.showTableView.nodataView.backgroundColor = UIColor.whiteColor;
    [self.showTableView removeMJRefresh];
    
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, self.showTableView.bottom, MY_Screen_W, 50)];
    bottomView.backgroundColor = UIColor.whiteColor;
    [self addSubview:bottomView];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = MyColorAlpha(@"000000", 0.4);
        self.top = 0;
    }];
}

#pragma mark - tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *content = self.dataArray[indexPath.row];
    static NSString *identifier = @"SelectTimeTypeCell";
    SelectTimeTypeCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[SelectTimeTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    [cell update:content];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(_selecttimeTypeBlock){
        _selecttimeTypeBlock(self.dataArray[indexPath.row][@"type"]);
    }
    [self btnClick:nil];
}

-(void)btnClick:(UIButton *)btn{
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = MyColorAlpha(@"000000", 0.4);
        self.top = MY_Screen_H;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 圆角角度
    CGFloat radius = 10.f;
    // 设置cell 背景色为透明
    cell.backgroundColor = UIColor.whiteColor;
    // 创建两个layer
    CAShapeLayer *normalLayer = [[CAShapeLayer alloc] init];
    CAShapeLayer *selectLayer = [[CAShapeLayer alloc] init];
    // 获取显示区域大小
    CGRect bounds = CGRectInset(cell.bounds, [UIScreen mainScreen].bounds.size.width/375 * 14, 0);
    // cell的backgroundView
    UIView *normalBgView = [[UIView alloc] initWithFrame:bounds];
    // 获取每组行数
    NSInteger rowNum = [tableView numberOfRowsInSection:indexPath.section];
    // 贝塞尔曲线
    UIBezierPath *bezierPath = nil;
    
    if (rowNum == 1) {
        // 一组只有一行（四个角全部为圆角）
        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
        normalBgView.clipsToBounds = NO;
    }else {
        normalBgView.clipsToBounds = YES;
        if (indexPath.row == 0) {
            normalBgView.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, 0, 0));
            CGRect rect = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, 0, 0));
            // 每组第一行（添加左上和右上的圆角）
            bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(radius, radius)];
        }else if (indexPath.row == rowNum - 1) {
            normalBgView.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, 0, 0));
            CGRect rect = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, 0, 0));
            // 每组最后一行（添加左下和右下的圆角）
            bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(radius, radius)];
        }else {
            // 每组不是首位的行不设置圆角
            bezierPath = [UIBezierPath bezierPathWithRect:bounds];
        }
    }
    
    // 阴影
    normalLayer.shadowColor = MyColor(@"#F1F1F1").CGColor;
    normalLayer.shadowOpacity = 0.2;
    normalLayer.shadowOffset = CGSizeMake(0, 0);
    normalLayer.path = bezierPath.CGPath;
    normalLayer.shadowPath = bezierPath.CGPath;
    
    // 把已经绘制好的贝塞尔曲线路径赋值给图层，然后图层根据path进行图像渲染render
    normalLayer.path = bezierPath.CGPath;
    selectLayer.path = bezierPath.CGPath;
    
    // 设置填充颜色
    normalLayer.fillColor = MyColor(@"#F1F1F1").CGColor;
    //    if (indexPath.row % 2 == 0) {
    //            normalLayer.fillColor = [UIColor whiteColor].CGColor;
    //        }else{
    //            normalLayer.fillColor = [UIColor colorWithHexString:@"e9e9e9"].CGColor;
    //        }
    // 添加图层到nomarBgView中
    [normalBgView.layer insertSublayer:normalLayer atIndex:0];
    normalBgView.backgroundColor = UIColor.clearColor;
    cell.backgroundView = normalBgView;
}

@end
